package sunyu.controller.shiro;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import sunyu.controller.CommonController;
import sunyu.pojo.TMenu;
import sunyu.pojo.easyui.Result;
import sunyu.pojo.easyui.TreeNode;
import sunyu.service.MenuService;
import sunyu.toolkit.core.LogKit;
import sunyu.toolkit.core.MapKit;
import sunyu.toolkit.core.StringKit;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜单控制器
 *
 * @author 孙宇
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends CommonController {

    private static final Logger logger = LogKit.getLogger();

    @Resource(name = "menuService")
    MenuService menuService;

    /**
     * 定义表单前缀
     *
     * @param binder
     */
    @InitBinder("tMenu")
    public void initBinderTmenu(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("tMenu.");
    }


    @RequestMapping("/tree")
    @ResponseBody
    public List<TreeNode> tree() {
        List<TreeNode> nodes = new ArrayList<>();
        for (TMenu m : menuService.select()) {
            TreeNode n = new TreeNode();
            n.setId(m.getId());
            n.setPid(m.getPid());
            n.setText(m.getName());
            n.setAttributes(MapKit.convert(m));
            nodes.add(n);
        }
        return nodes;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String treeGridPage() {
        return "/template/menu/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public List<TMenu> treeGrid() {
        List<TMenu> l = menuService.select();
        Collections.sort(l, (o1, o2) -> {
            if (o1.getSort() == null || o2.getSort() == null) {
                return 0;
            }
            if (o1.getSort().intValue() == o2.getSort().intValue()) {
                return 0;
            }
            return o1.getSort().intValue() > o2.getSort().intValue() ? 1 : -1;
        });
        return l;
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String formPage(Model model, @RequestParam(required = false) String id) {
        if (StringKit.isNotBlank(id)) {
            model.addAttribute("tMenu", menuService.selectByPrimaryKey(id));
        }
        return "/template/menu/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ModelAttribute("tMenu") TMenu tMenu) {
        Result r = new Result();
        menuService.insertSelective(tMenu);
        r.setSuccess(true);
        return r;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result edit(@ModelAttribute("tMenu") TMenu tMenu) {
        Result r = new Result();
        menuService.updateByPrimaryKey(tMenu);
        r.setSuccess(true);
        return r;
    }

    @RequestMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        Result r = new Result();
        r.setSuccess(true);
        return r;
    }

}
